package com.vanzo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.vanzo.bean.Follower;
import com.vanzo.encryption.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tisen on 2018/3/9.
 */

public class TalkieFollowerAdapter extends RecyclerView.Adapter<TalkieFollowerAdapter.FollowerView> {

    private Context context;
    private List<Follower> followers;
    private FollowerClickListener listener;


    public TalkieFollowerAdapter(Context context) {
        this.context = context;
        followers = new ArrayList<>();
    }

    public void setListener(FollowerClickListener listener) {
        this.listener = listener;
    }

    public void setFollowers(List<Follower> followers) {
        this.followers = followers;
        notifyDataSetChanged();
    }


    @Override
    public FollowerView onCreateViewHolder(ViewGroup parent, int viewType) {
        return new FollowerView(LayoutInflater.from(parent.getContext()).inflate(R.layout.follower_item_view, parent, false));
    }

    @Override
    public void onBindViewHolder(FollowerView holder, int position) {
        holder.setFollower(followers.get(position), position);
    }

    @Override
    public int getItemCount() {
        return followers.size();
    }

    public class FollowerView extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView terminal;
        private ImageView nfcBind;
        private ImageView message;
        private ImageView voice;
        private ImageView delete;
        private int position;

        public FollowerView(View itemView) {
            super(itemView);
//            itemView.setOnClickListener(this);
            terminal = (TextView) itemView.findViewById(R.id.follower_terminal_id);
            nfcBind = (ImageView) itemView.findViewById(R.id.follower_nfc_bind);
            message = (ImageView) itemView.findViewById(R.id.follower_message);
            voice = (ImageView) itemView.findViewById(R.id.follower_voice);
            delete = (ImageView) itemView.findViewById(R.id.follower_delete);
        }

        public void setFollower(Follower follower, int position) {
            this.position = position;
            if (position == 0) {
                terminal.setText("机主");
                delete.setVisibility(View.INVISIBLE);
            } else {
                terminal.setText(follower.getTerminalId() + "");
                delete.setVisibility(View.VISIBLE);
            }
            nfcBind.setOnClickListener(this);
            message.setOnClickListener(this);
            voice.setOnClickListener(this);
            delete.setOnClickListener(this);
            delete.setImageResource(follower.isKillStatus() ? R.drawable.contacts_delete_normal : R.drawable.contacts_delete_pressed);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                switch (v.getId()) {
                    case R.id.follower_nfc_bind:
                        listener.onNFCBindClick(position);
                        break;
                    case R.id.follower_message:
                        listener.onMessageClick(position);
                        break;
                    case R.id.follower_voice:
                        listener.onVoiceClick(position);
                        break;
                    case R.id.follower_delete:
                        listener.onDeleteClick(position);
                        break;
                }
            }
        }
    }

    public interface FollowerClickListener {
        void onNFCBindClick(int position);

        void onMessageClick(int position);

        void onVoiceClick(int position);

        void onDeleteClick(int position);
    }
}
